package com.example.springbootdemo.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;

/**
 * @Description: springbootdemo
 * @Author: Anonymous
 * @Time: 2019-03-21 17:10
 */
@Component
public class RedisUtil {

    @Autowired
    private StringRedisTemplate redisTemplate;

    public void setData(String key, Object value) {
        if (value instanceof String) {
            redisTemplate.opsForValue().set(key, String.valueOf(value));
        } else if (value instanceof List) {
            redisTemplate.opsForList().rightPushAll(key, (Collection<String>) value);
        }
    }

    public String getString(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public List<String> getList(String key) {
        List<String> data = redisTemplate.opsForList().range(key, 0, -1);
        return data;
    }

    public void remove(String key) {
        redisTemplate.delete(key);
    }
}
